import { Component, Vue } from 'vue-property-decorator';
import { Action, Getter } from 'vuex-class';
import { LoginState } from '@/store/module/login/login.interface';

@Component({
  components: {}
})
export default class Login extends Vue {
  private form: any;
  private loginErrorMsg: boolean = false;
  private errorMsg: string = '';

  @Action
  private login!: (data: LoginState) => Promise<any>;

  private created() {}

  private beforeCreate() {
    this.form = this.$form.createForm(this);
  }

  private closeLoginMsg() {
    this.loginErrorMsg = false;
  }

  private handleSubmit(e: any) {
    e.preventDefault();
    this.form.validateFields((err: any, values: any) => {
      if (!err) {
        console.log('Received values of form: ', values);
        this.login(values).then(res => {
          console.log('res>>>>', res.data);
          if (res.data.success) {
            // 将token添加到localstage里面去
            localStorage.setItem('token', res.data.data);
            this.$router.push('home');
          } else {
            this.loginErrorMsg = true;
            this.errorMsg = res.data.errorMsg;
          }
        });
      }
    });
  }
  private goRegister(e: any) {
    this.$router.push('register');
  }

  private onShowSizeChange(current: any, pageSize: any) {
    console.log(current, pageSize);
  }
}
